﻿using System;
using System.Drawing;
using System.Web.Mvc;
using ControlModule.Daos;
using ControlModule.Models;
using ControlModule.QuizModule;
using ControlModule.Security;

namespace ControlModule.Controllers
{
    public class QuizController : Controller
    {
        private static QuizTestItem _nextQuizTestItem = null;
        const int percentOfSuccesfulQuiz = 25;
        //
        // GET: /Admission/

        [Authorize(Roles = "STUDENT")]
        public ActionResult Instruct(int labworkId)
        {
            if (FullLabworkDao.IfQuizofLabWorkComplete(labworkId, MyMembershipProvider.GetCurrentUserInstance().Id))
            {
                return RedirectToAction("CabinetPage", "Personal");
            }
            Labwork labwork = FullLabworkDao.GetLabworkById((int)labworkId);
            if (labwork != null)
            {
                QuizManager.Get().LoadQuestionForLabwork((int)labworkId);
                return View(labwork);
            }
            return RedirectToAction("CabinetPage", "Personal");
        }

        [Authorize(Roles = "STUDENT")]
        public ActionResult Test(int labworkId)
        {
            if ((QuizManager.Get().IsQuestionLoaded() && _nextQuizTestItem == null) || !QuizManager.Get().IsQuestionLoaded())
            {
                QuizManager.Get().LoadQuestionForLabwork(labworkId);
                _nextQuizTestItem = QuizManager.Get().ReadNext();
            }

            if (_nextQuizTestItem != null)
                return View(_nextQuizTestItem);
            return RedirectToAction("Result", "Quiz", new { labworkId });
        }

        [Authorize(Roles = "STUDENT")]
        [HttpPost]
        public ActionResult Test(int labworkId, QuizTestItem quizTestItem)
        {
            if (quizTestItem != null)
            {
                RelLabworkUserQuestionAnswer item = new RelLabworkUserQuestionAnswer
                {
                    LabworkId = (int)labworkId,
                    UserId = MyMembershipProvider.GetCurrentUserInstance().Id,
                    QuestionId = quizTestItem.Id,
                    AnswerId = quizTestItem.SelectAnswer,
                    DatePublic = DateTime.Now
                };
                using (CmDbEntities db = new CmDbEntities())
                {
                    db.AddToRelLabworkUserQuestionAnswer(item);
                    db.SaveChanges();
                }
                _nextQuizTestItem = QuizManager.Get().ReadNext();

                if (_nextQuizTestItem == null)
                    return RedirectToAction("Result", "Quiz", new { labworkId });
            }

            return RedirectToAction("Test", new { labworkId });
        }

        public ActionResult Result(int labworkId)
        {
            _nextQuizTestItem = null;

            Point result = FullLabworkDao.GetStatusOfFinishedQuiz(
                labworkId, MyMembershipProvider.GetCurrentUserInstance().Id);

            if (((float)result.X) / 4 > result.Y)
                ViewData["LabAccess"] = "false";
            else
                ViewData["LabAccess"] = "true";

            ViewData["Total"] = result.X;
            ViewData["Success"] = result.Y;

            Labwork labwork = FullLabworkDao.GetLabworkById(labworkId);
            return View(labwork);
        }
    }
}
